﻿using Leadtools.DicomDemos;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DICOMViewer.Utility;

namespace DICOMViewer.UC
{
    public partial class ServerSettingForm : Form
    {
        public ServerSettingForm()
        {
            InitializeComponent();
        }

        private void rItemBtn_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            string IP = gridView_Server.GetFocusedRowCellValue("Ip").ToString();
            //删除操作
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Delete)
            {
                DialogResult dialog = MessageBox.Show("是否删除该设置？", "确认", MessageBoxButtons.YesNo );
                if (dialog == DialogResult.Yes)
                {
                    
                    serverList.Remove(serverList.First(o => o.Ip == IP));
                }
            }
            else if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis)
            {
                ServerInfo serverInfo = serverList.Find(o=>o.Ip == IP);
                ServerSettingEditForm form = new ServerSettingEditForm(serverInfo,serverList);
                form.ShowDialog();
            }
            this.gridControl_Server.RefreshDataSource();
        }

        List<ServerInfo> serverList = new List<ServerInfo>();
        private void ServerSettingForm_Load(object sender, EventArgs e)
        {
            foreach (ServerInfo serverInfo in ComSettingReader.getClientConfig().Servers.ToArray())
            {
                serverList.Add((ServerInfo)serverInfo.Clone());
            }
            this.gridControl_Server.DataSource = serverList;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ComSettingReader.SaveServerConfig(serverList.ToArray());
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ServerSettingEditForm form = new ServerSettingEditForm(serverList);
            form.ShowDialog();
            this.gridControl_Server.RefreshDataSource();
        }

        private void btnLocalSetting_Click(object sender, EventArgs e)
        {
            LocalSettingForm form = new LocalSettingForm(Utility.ComSettingReader.getClientConfig());
            form.ShowDialog();
        }
    }
}
